import { Injectable } from '@angular/core';
import {ConfigService} from './config.service';
import {HttpClient} from '@angular/common/http';
import {News} from '../model/News';
import {NewsCate} from '../model/NewsCate';

@Injectable({
  providedIn: 'root'
})
export class NewsService {

  constructor(private config:ConfigService,private http:HttpClient) { }
  private getNewsListUrl=this.config.HOST+"/public/getNewsList";


  getNewsList(){
    return this.http.get(this.getNewsListUrl).toPromise();
  }
  private getNewsUrl=this.config.HOST+"/public/getNews";
  getNews(id:string){
    let parm={
      "newsid": id
    }
    return this.http.post(this.getNewsUrl,parm).toPromise();
  }
  private getNewsListByPageUrl=this.config.HOST+"/public/getNewsListByPage";
  getNewsListByPage(page:number,pagesize:number){
    let parm={
      "page":page,
      "pagesize":pagesize

    }
    return this.http.post(this.getNewsListByPageUrl,parm).toPromise();
  }

  private saveNewsUrl=this.config.HOST+"/manage/saveNews"
  saveNews(news:News){

    return this.http.post(this.saveNewsUrl,news).toPromise();
  }

  private deleteNewsUrl=this.config.HOST+"/manage/deleteNews";
  deleteNews(id:string){
    let parm={
      "newsid": id

    }

    return this.http.post(this.deleteNewsUrl,parm).toPromise();
  }


  public getNewsCategory():Array<NewsCate>{
    let cl=new Array();
    for(let i=0;i<10;i++){
      let c=new NewsCate();
      c.newscateid="newscateid"+i;
      c.name="目录"+i;
      cl.push(c);
    }
    return cl;

  }

  private getNewsListByCateUrl=this.config.HOST+"/public/getNewsListByCate";
  getNewsListByCate(cate:string
  ,page:number
  ,pagesize:number){

    let parm={
      "page":page,
      "pagesize":pagesize,
      "newscate":cate
    }
    return this.http.post(this.getNewsListByCateUrl,parm).toPromise();


  }

  private uplaodPicUrl=this.config.HOST+"/manage/uploadPic";
  uploadPic(file:string){

    const uploadData = new FormData();
    uploadData.append('uploadfile', file);
    return this.http.post(this.uplaodPicUrl,uploadData).toPromise();

  }

}
